<?php
/**
 * Created by PhpStorm.
 * User: Da Xiong
 * Date: 2018/12/5
 * Time: 14:43
 */

namespace App\Http\Controllers;


use App\Models\Address;

class AddressController extends Controller
{
    //获取个人地址列表信息
    public function index(){
        $uid = auth()->guard('api')->id();
        $list = Address::where('user_id',$uid)
            ->orderBy('updated_at','DESC')
            ->orderBy('lately','DESC')
            ->get();
        return $this->returnJson(0,'',$list);
    }

    //更新个人地址列表信息
    public function update(){
        $uid = auth()->guard('api')->id();
        $address_id = (int)request('id',0);
        $params = [
            'name' => (string)request('name',''),
            'address' => (string)request('address',''),
            'phone' => (string)request('phone',''),
            'lately' => Address::STATUS_1,
        ];
        Address::where('user_id',$uid)->update(['lately' => Address::STATUS_0]);
        if($address_id > 0){
            //更新
            $addressInfo = Address::find($address_id);
            if($addressInfo){
                $addressInfo->update($params);
                return $this->returnJson(0);
            }else{
                return $this->returnJson(1,'error');
            }
        }else{
            //新增
            $params['user_id'] = $uid;
            $createID = Address::create($params);
            if($createID){
                return $this->returnJson(0);
            }else{
                return $this->returnJson(1,'error');
            }
        }
    }

}